<?php declare(strict_types=1);


namespace app\model;

class UserToken extends Base {

    protected $name = 'user_token';

    // 设置字段信息
    protected $schema = [
        'user_id'     => 'int',
        'token'       => 'string',
        'expire_at' => 'datetime',
        'create_at' => 'datetime',
        'update_at' => 'datetime',
    ];

    // 获取Token
    public function getTokenByToken(string $token) : ?UserToken{
        $token =  $this->where('token','=',trim($token))->find();
        return $token;
    }

    // 获取token
    public function getTokenByUserId(string $id) : ?UserToken  {
       $token =  $this->where('user_id','=',$id)->find();
       return $token;
    }

    // Delete Token Info By User ID
    public function deleteTokenByUserId(int $userId) : bool {

       $userToken = $this->where('user_id','=',$userId)->find();
       $result =  $userToken->force()->delete();
       $result = $result >=1 ? true : false;
       return $result;
    }
}

 
